<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img src="../../assets/images/logo.png" alt="" />
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#23262E"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img v-if="userInfo.user_pic" :src="userInfo.user_pic" alt="" class="avatar" />
            <img v-else src="../../assets/logo.png" alt="" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"><i class="el-icon-s-operation"></i>基本资料</el-menu-item>
          <el-menu-item index="1-2"><i class="el-icon-camera"></i>更换头像</el-menu-item>
          <el-menu-item index="1-3"><i class="el-icon-key"></i>重置密码</el-menu-item>
        </el-submenu>
        <el-menu-item index="2" @click="exit"><i class="el-icon-switch-button"></i>退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <!-- 左侧边栏区域 -->
<el-aside width="200px">
  <div class="user-box">
    <img v-if="userInfo.user_pic" :src="userInfo.user_pic" alt="" />
    <img v-else src="../../assets/logo.png" alt="" />
    <!-- 如果有nickname就优先使用nickname,没有就显示用户名 -->
    <span>欢迎 {{userInfo.nickname ? userInfo.nickname : userInfo.username}}</span>
  </div>
   <el-menu
  default-active="/home"
  class="el-menu-vertical-demo"
  background-color="#23262E"
  text-color="#fff"
  active-text-color="#409EFF"
  unique-opened
  router
>
<!-- menus数组内有3个元素,每个元素是一个对象 -->
<!-- 对象内有children属性,如果属性值null,说明没有子菜单 -->
<!-- 对象内有children属性,如果属性值有属性,说明有子菜单 -->

  <template v-for="item in menus">
    <!-- 不包含子菜单的“一级菜单” -->
  <el-menu-item :index="item.indexPath" :key="item.indexPath" v-if="!item.children">
    <i :class="item.icon"></i>{{item.title}}</el-menu-item>
  <!-- 包含子菜单的“一级菜单” -->
  <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
    <template slot="title">
      <i :class="item.icon"></i>
      <span>{{item.title}}</span>
    </template>
    <el-menu-item 
      :index="ele.indexPath" v-for="ele in item.children" :key="ele.indexPath"><i :class="ele.icon"></i>{{ele.title}}</el-menu-item>
  </el-submenu>
  </template>
</el-menu>

</el-aside>
      <el-container>
        <!-- 页面主体区域 -->
        <el-main>
          <!-- Main路由出口 -->
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© www.itheima.com - 黑马程序员</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import {mapState} from 'vuex'
export default {
  name: 'Main',
  data(){
    return{
      menus:[]
    }
  },
  computed:{
    ...mapState(['userInfo','token'])
  },
  methods: {
    // 退出
    exit(){
       this.$confirm('此操作将永久退出, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          //清空token
          //触发vuex里面的mutations里面的updateToken函数
          this.$store.commit('updateToken', '');
          // 跳转登录页面
          this.$router.push('/login')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          });          
        });
    },
    // 获取左侧侧边栏菜单数据
   async getAside(){
    //  console.log(1113222);
    const {data:res} = await this.$http.get('/my/menus',{headers: {
      Authorization: this.$store.state.token
    } }) 
       if (res.code === 0) {
    this.menus = res.data
    }
  }
  },
  created () {
    // created生命周期函数中，调用 Vuex 中的 initUserInfo 函数，获取用户的基本信息
    this.$store.dispatch('initUserInfo')
    this.getAside() 
// 判断 token 是否有值，并进行不同的处理
 if (this.token) {
    // 如果 token 有值，就请求数据
    this.getAside()
    this.$store.dispatch('initUserInfo')
  } else {
    // 如果 token 没有值，就强制跳转到登录页面
    this.$router.push('/login')
  }

  }
}
</script>

<style lang="less" scoped>
.main-container {
  height: 100%;
  .el-header,
  .el-aside {
    background-color: #23262e;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #F2F2F2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}
// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}
// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
  }
}
</style>
